#include <iostream>
using namespace std;
#include <signal.h>
#include<ctime>
#include <unistd.h>
#include <sys/wait.h>
int flag=0;

void handler(int signo)
{
    sleep(2);
    //信号方式等待
    //非阻塞轮询
    pid_t id;
   while((id=waitpid(-1,nullptr,WNOHANG))>0)
   {
    cout<<"signo is: "<<signo<<"wait proecess id is:"<<id<<endl;

        cout<<"process quit!!"<<endl;
    
   }
}
void handler1(int signo)
{
    cout<<"signo"<<signo<<endl;
    flag=1;
}
int main()
{
    srand(time(nullptr));
    signal(17,handler);
    signal(2,handler1);
    //如果十个子进程同时退出或者退一半
    for(int i=0;i<10;i++)
    {
    pid_t id=fork();
    if(id==0)
    {
        //child
        while(true)
        {
            cout<<"i am child process,process id is:"<<getpid()<<"  ppid is:"<<getppid()<<endl;
            sleep(1);
            break;
        }
        exit(0);
    }
    sleep(rand()%5+3);
    }
    sleep(1);
    //farther
    while(!flag)
    {
        cout<<"i am farther process,process id is:"<<getpid()<<endl;
        sleep(1);
    }
    return 0;
}